<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="keywords" content="玖忆;文鹤;博客">
    
    <meta name="author" content="meteor">
    <!-- preconnect -->
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    
    
    <!--- Seo Part-->
    
    <link rel="canonical" href="https://wait-you.github.io/2023/09/14/gradle依赖管理的三种方式/"/>
    <meta name="robots" content="index,follow">
    <meta name="googlebot" content="index,follow">
    <meta name="revisit-after" content="1 days">
    
        <meta name="description" content="# Gradle 依赖管理的三种方式 # ext 在项目的 build.gradle  文件中使用 ext  块来定义全局变量，然后在依赖中引用这些变量。这是一种简单的方式来管理依赖版本。 123456789101112&#x2F;&#x2F; 在build.gradle中定义全局变量ext &amp;#123;    &#x2F;&#x2F; 依赖版本    springVersion &#x3D; &amp;#x27;5.2.10.RELEASE&amp;#x27;">
<meta property="og:type" content="article">
<meta property="og:title" content="Gradle依赖管理的三种方式">
<meta property="og:url" content="https://wait-you.github.io/2023/09/14/Gradle%E4%BE%9D%E8%B5%96%E7%AE%A1%E7%90%86%E7%9A%84%E4%B8%89%E7%A7%8D%E6%96%B9%E5%BC%8F/index.html">
<meta property="og:site_name" content="玖忆">
<meta property="og:description" content="# Gradle 依赖管理的三种方式 # ext 在项目的 build.gradle  文件中使用 ext  块来定义全局变量，然后在依赖中引用这些变量。这是一种简单的方式来管理依赖版本。 123456789101112&#x2F;&#x2F; 在build.gradle中定义全局变量ext &amp;#123;    &#x2F;&#x2F; 依赖版本    springVersion &#x3D; &amp;#x27;5.2.10.RELEASE&amp;#x27;">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://tuchuang.wenhe9.cn/img/202309122348069.png">
<meta property="article:published_time" content="2023-09-14T08:27:15.000Z">
<meta property="article:modified_time" content="2023-09-13T16:30:49.661Z">
<meta property="article:author" content="meteor">
<meta property="article:tag" content="Gradle">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://tuchuang.wenhe9.cn/img/202309122348069.png">
    
    
    <!--- Icon Part-->
    <link rel="icon" type="image/png" href="/images/wenhe.png" sizes="192x192">
    <link rel="apple-touch-icon" sizes="180x180" href="/images/wenhe.png">
    <meta name="theme-color" content="#f1404b">
    <link rel="shortcut icon" href="/images/wenhe.png">
    <!--- Page Info-->
    
    <title>
        
            Gradle依赖管理的三种方式 -
        
        玖忆
    </title>
    
<link rel="stylesheet" href="/css/style.css">

    
<link rel="stylesheet" href="/assets/fonts.css">

    <!--- Font Part-->
    
    
    
    

    <!--- Inject Part-->
    
    <script id="hexo-configurations">
    let Global = window.Global || {};
    Global.hexo_config = {"hostname":"wait-you.github.io","root":"/","language":"zh-CN"};
    Global.theme_config = {"articles":{"style":{"font_size":"16px","line_height":1.5,"image_border_radius":"14px","image_alignment":"center","image_caption":false,"link_icon":true},"word_count":{"enable":true,"count":true,"min2read":true},"author_label":{"enable":true,"auto":false,"list":[]},"code_block":{"copy":true,"style":"mac","font":{"enable":false,"family":null,"url":null}},"toc":{"enable":true,"max_depth":3,"number":false,"expand":true,"init_open":true},"copyright":true,"lazyload":true,"recommendation":{"enable":false,"title":"推荐阅读","limit":3,"placeholder":"http://tuchuang.wenhe9.cn/default-bg.jpg","skip_dirs":[]}},"colors":{"primary":"#f1404b","secondary":null},"global":{"fonts":{"chinese":{"enable":false,"family":null,"url":null},"english":{"enable":false,"family":null,"url":null}},"content_max_width":"1000px","sidebar_width":"210px","hover":{"shadow":true,"scale":false},"scroll_progress":{"bar":true,"percentage":true},"busuanzi_counter":{"enable":true,"site_pv":true,"site_uv":true,"post_pv":true},"pjax":true,"open_graph":true,"google_analytics":{"enable":false,"id":null}},"home_banner":{"enable":true,"style":"static","image":{"light":"http://tuchuang.wenhe9.cn/default-bg.jpg","dark":"http://tuchuang.wenhe9.cn/default-bg.jpg"},"title":"玖忆","subtitle":{"text":["我本微末凡尘、可也心向天空"],"hitokoto":{"enable":false,"api":"https://v1.hitokoto.cn"},"typing_speed":100,"backing_speed":80,"starting_delay":500,"backing_delay":1500,"loop":true,"smart_backspace":true},"text_color":{"light":"#fff","dark":"#d1d1b6"},"text_style":{"title_size":"2.8rem","subtitle_size":"1.5rem","line_height":1.2},"custom_font":{"enable":false,"family":null,"url":null},"social_links":{"enable":true,"links":{"github":"https://gitee.com/du-jinliang","instagram":null,"zhihu":null,"twitter":null,"email":"dujinliang9@163.com"}}},"plugins":{"feed":{"enable":false},"aplayer":{"enable":false,"type":"fixed","audios":[{"name":null,"artist":null,"url":null,"cover":null}]},"mermaid":{"enable":false,"version":"9.3.0"}},"version":"2.1.4","navbar":{"auto_hide":true,"color":{"left":"#f78736","right":"#367df7","transparency":35},"links":{"Home":{"path":"/","icon":"fa-regular fa-house"}},"search":{"enable":false,"preload":true}},"page_templates":{"friends_column":2,"tags_style":"blur"},"home":{"sidebar":{"enable":true,"position":"left","first_item":"menu","announcement":null,"links":null},"article_date_format":"auto","categories":{"enable":true,"limit":3},"tags":{"enable":true,"limit":3}}};
    Global.language_ago = {"second":"%s 秒前","minute":"%s 分钟前","hour":"%s 小时前","day":"%s 天前","week":"%s 周前","month":"%s 个月前","year":"%s 年前"};
    Global.data_config = {"masonry":false};
  </script>
    
    <!--- Fontawesome Part-->
    
<link rel="stylesheet" href="/fontawesome/fontawesome.min.css">

    
<link rel="stylesheet" href="/fontawesome/brands.min.css">

    
<link rel="stylesheet" href="/fontawesome/solid.min.css">

    
<link rel="stylesheet" href="/fontawesome/regular.min.css">

    
    
    
    
<meta name="generator" content="Hexo 6.3.0"></head>


<body>
<div class="progress-bar-container">
    
        <span class="scroll-progress-bar"></span>
    

    
        <span class="pjax-progress-bar"></span>
        <span class="pjax-progress-icon">
            <i class="fa-solid fa-circle-notch fa-spin"></i>
        </span>
    
</div>


<main class="page-container">

    

    <div class="main-content-container">

        <div class="main-content-header">
            <header class="navbar-container">
    
    <div class="navbar-content">
        <div class="left">
            
            <a class="logo-title" href="/">
                
                玖忆
                
            </a>
        </div>

        <div class="right">
            <!-- PC -->
            <div class="desktop">
                <ul class="navbar-list">
                    
                        
                            <li class="navbar-item">
                                <!-- Menu -->
                                <a class="" 
                                    href="/"  >
                                    
                                        
                                            <i class="fa-regular fa-house"></i>
                                        
                                        首页
                                    
                                </a>
                                <!-- Submenu -->
                                
                            </li>
                    
                    
                </ul>
            </div>
            <!-- Mobile -->
            <div class="mobile">
                
                <div class="icon-item navbar-bar">
                    <div class="navbar-bar-middle"></div>
                </div>
            </div>
        </div>
    </div>

    <!-- Mobile drawer -->
    <div class="navbar-drawer">
        <ul class="drawer-navbar-list">
            
                
                    <li class="drawer-navbar-item flex-center">
                        <a class="" 
                        href="/"  >
                             
                                
                                    <i class="fa-regular fa-house"></i>
                                
                                首页
                            
                        </a>
                    </li>
                    <!-- Submenu -->
                    
            

        </ul>
    </div>

    <div class="window-mask"></div>

</header>


        </div>

        <div class="main-content-body">

            

            <div class="main-content">

                
                    <div class="fade-in-down-animation">
    <div class="post-page-container">
        <div class="article-content-container">

            
            
                <div class="article-title">
                    <h1 class="article-title-regular">Gradle依赖管理的三种方式</h1>
                </div>
            
                
            

            
                <div class="article-header">
                    <div class="avatar">
                        <img src="/images/wenhe.png">
                    </div>
                    <div class="info">
                        <div class="author">
                            <span class="name">meteor</span>
                            
                                <span class="author-label">Lv3</span>
                            
                        </div>
                        <div class="meta-info">
                            <div class="article-meta-info">
    <span class="article-date article-meta-item">
        <i class="fa-regular fa-pen-fancy"></i>&nbsp;
        <span class="desktop">2023-09-14 00:27:15</span>
        <span class="mobile">2023-09-14 00:27</span>
        <span class="hover-info">创建</span>
    </span>
    
        <span class="article-date article-meta-item">
            <i class="fa-regular fa-wrench"></i>&nbsp;
            <span class="desktop">2023-09-13 08:30:49</span>
            <span class="mobile">2023-09-13 08:30</span>
            <span class="hover-info">更新</span>
        </span>
    

    
        <span class="article-categories article-meta-item">
            <i class="fa-regular fa-folders"></i>&nbsp;
            <ul>
                
                    <li>
                        <a href="/categories/Gradle/">Gradle</a>&nbsp;
                    </li>
                
            </ul>
        </span>
    
    
        <span class="article-tags article-meta-item">
            <i class="fa-regular fa-tags"></i>&nbsp;
            <ul>
                
                    <li>
                        <a href="/tags/Gradle/">Gradle</a>&nbsp;
                    </li>
                
            </ul>
        </span>
    

    
    
    
    
        <span class="article-pv article-meta-item">
            <i class="fa-regular fa-eye"></i>&nbsp;<span id="busuanzi_value_page_pv"></span>
        </span>
    
</div>

                        </div>
                    </div>
                </div>
            

            <div class="article-content markdown-body">
                <h2 id="gradle依赖管理的三种方式"><a class="markdownIt-Anchor" href="#gradle依赖管理的三种方式">#</a> Gradle 依赖管理的三种方式</h2>
<h3 id="ext"><a class="markdownIt-Anchor" href="#ext">#</a> ext</h3>
<p>在项目的 <code>build.gradle</code>  文件中使用 <code>ext</code>  块来定义全局变量，然后在依赖中引用这些变量。这是一种简单的方式来管理依赖版本。</p>
<div class="highlight-container" data-rel="Groovy"><figure class="iseeu highlight groovy"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 在build.gradle中定义全局变量</span></span><br><span class="line">ext &#123;</span><br><span class="line">    <span class="comment">// 依赖版本</span></span><br><span class="line">    springVersion = <span class="string">&#x27;5.2.10.RELEASE&#x27;</span></span><br><span class="line">    junitVersion = <span class="string">&#x27;5.7.0&#x27;</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">dependencies &#123;</span><br><span class="line">    implementation <span class="string">&quot;org.springframework:spring-core:$springVersion&quot;</span></span><br><span class="line">    testImplementation <span class="string">&quot;org.junit.jupiter:junit-jupiter-api:$junitVersion&quot;</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure></div>
<p>也可以单独定义一个 <code>versions.gralde</code>  文件，在其中配置版本信息</p>
<div class="highlight-container" data-rel="Groovy"><figure class="iseeu highlight groovy"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">ext &#123;</span><br><span class="line">    </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></div>
<p>然后在模块下的 build.gradle 使用则通过  <code>apply</code>  方式引入。 添加</p>
<div class="highlight-container" data-rel="Groovy"><figure class="iseeu highlight groovy"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">apply from <span class="string">&quot;versions.gradle&quot;</span></span><br></pre></td></tr></table></figure></div>
<p>在 <code>build.gradle.ktx</code>  中可以这样写</p>
<div class="highlight-container" data-rel="Kotlin"><figure class="iseeu highlight kotlin"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">buildscript &#123;</span><br><span class="line">    extra[<span class="string">&quot;springBootVersion&quot;</span>] = <span class="string">&quot;3.0.0&quot;</span></span><br><span class="line">    extra[<span class="string">&quot;springCloudVersion&quot;</span>] = <span class="string">&quot;2022.0.3&quot;</span></span><br><span class="line">    extra[<span class="string">&quot;springCloudAlibabaVersion&quot;</span>] = <span class="string">&quot;2022.0.0.0-RC2&quot;</span></span><br><span class="line">    extra[<span class="string">&quot;lombokVersion&quot;</span>] = <span class="string">&quot;1.18.28&quot;</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">allprojects &#123;</span><br><span class="line">    ...</span><br><span class="line"></span><br><span class="line">    configure&lt;DependencyManagementExtension&gt; &#123;</span><br><span class="line">        imports &#123;</span><br><span class="line">            mavenBom(<span class="string">&quot;org.springframework.cloud:spring-cloud-dependencies:<span class="subst">$&#123;property(<span class="string">&quot;springCloudVersion&quot;</span>)&#125;</span>&quot;</span>)</span><br><span class="line">            mavenBom(<span class="string">&quot;com.alibaba.cloud:spring-cloud-alibaba-dependencies:<span class="subst">$&#123;property(<span class="string">&quot;springCloudAlibabaVersion&quot;</span>)&#125;</span>&quot;</span>)</span><br><span class="line">        &#125;</span><br><span class="line">        dependencies &#123;</span><br><span class="line">            dependency(<span class="string">&quot;org.projectlombok:lombok:<span class="subst">$&#123;property(<span class="string">&quot;lombokVersion&quot;</span>)&#125;</span>&quot;</span>)</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    ...</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></div>
<p>优点：</p>
<ul>
<li>简单易懂，适用于小型项目。</li>
<li>可以在一个地方集中管理依赖版本。</li>
</ul>
<p>缺点</p>
<ul>
<li>不适用于大型项目，容易导致 <code>build.gradle</code>  文件变得臃肿。</li>
<li>不支持适应性版本解析（如动态版本号）。</li>
</ul>
<h3 id="buildsrc"><a class="markdownIt-Anchor" href="#buildsrc">#</a> buildSrc</h3>
<blockquote>
<p>当运行 Gradle 时会检查项目中是否存在一个名为 buildSrc 的目录。然后 Gradle 会自动编译并测试这段代码，并将其放入构建脚本的类路径中，对于多项目构建，只能有一个 buildSrc 目录，该目录必须位于根项目目录中，buildSrc 是 Gradle 项目根目录下的一个目录，它可以包含我们的构建逻辑，与脚本插件相比，buildSrc 应该是首选，因为它更易于维护、重构和测试代码</p>
</blockquote>
<p>适用于中大型项目，特别是当多个子项目需要共享相同的依赖版本时。</p>
<p>使用单独的 <code>buildSrc</code>  项目来管理依赖，这个项目包含了构建脚本和依赖定义。每个子项目可以引用 <code>buildSrc</code>  中的依赖。</p>
<ol>
<li>创建 <code>buildSrc</code>  项目：在项目根目录下创建一个名为 <code>buildSrc</code>  的文件夹，Gradle 会自动识别它并将其作为构建脚本的一部分。</li>
<li>在 <code>buildSrc</code>  项目中创建 Groovy 或 Kotlin 类（通常在 <code>src/main/groovy</code>  或 <code>src/main/kotlin</code>  目录下），用于管理依赖。这些类可以包含依赖版本号的定义。</li>
<li>在项目的构建脚本中，可以直接引用 <code>buildSrc</code>  中定义的类来获取依赖版本号。</li>
</ol>
<p>示例：</p>
<p>在 <code>buildSrc/src/main/groovy</code>  目录下创建一个名为 <code>Dependencies.groovy</code>  的 Groovy 类，用于定义依赖版本号：</p>
<div class="highlight-container" data-rel="Groovy"><figure class="iseeu highlight groovy"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// buildSrc/src/main/groovy/Dependencies.groovy</span></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Dependencies</span> &#123;</span><br><span class="line">    <span class="keyword">static</span> <span class="keyword">final</span> String springVersion = <span class="string">&#x27;5.2.10.RELEASE&#x27;</span></span><br><span class="line">    <span class="keyword">static</span> <span class="keyword">final</span> String junitVersion = <span class="string">&#x27;5.7.0&#x27;</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure></div>
<p>然后，在项目的 <code>build.gradle</code>  文件中，你可以引用这些版本号：</p>
<div class="highlight-container" data-rel="Groovy"><figure class="iseeu highlight groovy"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 项目的 build.gradle 文件</span></span><br><span class="line">dependencies &#123;</span><br><span class="line">    implementation <span class="string">&quot;org.springframework:spring-core:$&#123;Dependencies.springVersion&#125;&quot;</span></span><br><span class="line">    testImplementation <span class="string">&quot;org.junit.jupiter:junit-jupiter-api:$&#123;Dependencies.junitVersion&#125;&quot;</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure></div>
<p>优点：</p>
<p>这种方式的优点是可以集中管理依赖版本，使构建脚本更加清晰，特别是在多个子项目需要使用相同的版本号时。这对于大型项目尤其有用，因为它可以减少重复工作和版本号不一致的问题。</p>
<p>缺点</p>
<p>需要额外的 <code>buildSrc</code>  项目，可能稍微复杂一些</p>
<h3 id="tomlversion-catalogs"><a class="markdownIt-Anchor" href="#tomlversion-catalogs">#</a> toml–Version Catalogs</h3>
<p>特点：</p>
<ul>
<li>对所有 module 可见，可统一管理所有 module 的依赖，Gradle 会为每个依赖目录生成一个类型安全的访问器，如：libs.coreKtx。 每个依赖目录对构建项目都是可见的，确保依赖项的版本适用于每个子项目或模块</li>
<li>依赖项除了可以声明为单个依赖目录，还可以将多个依赖项声明为依赖目录组。即支持声明依赖 bundles, 即总是一起使用的依赖可以组合在一起，</li>
<li>支持版本号与依赖名分离，可以在多个依赖间共享版本号</li>
<li>支持在单独的 libs.versions.toml 文件中配置依赖</li>
<li>支持在项目间共享依赖</li>
</ul>
<h4 id="通过-settingsgradlektx-声明-version-catalogs"><a class="markdownIt-Anchor" href="#通过-settingsgradlektx-声明-version-catalogs">#</a> 通过 settings.gradle.ktx 声明 version catalogs</h4>
<p>在  <code>settings.gradle.ktx</code>  中声明</p>
<div class="highlight-container" data-rel="Kotlin"><figure class="iseeu highlight kotlin"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">dependencyResolutionManagement &#123; </span><br><span class="line">    versionCatalogs &#123; </span><br><span class="line">        create(<span class="string">&quot;libs&quot;</span>) &#123;</span><br><span class="line">            library(<span class="string">&quot;springBoot&quot;</span>, <span class="string">&quot;org.springframework.boot&quot;</span>, <span class="string">&quot;spring-boot-gradle-plugin&quot;</span>).version(<span class="string">&quot;3.0.0&quot;</span>)</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></div>
<p>在 <code>build.gralde.ktx</code>  中使用</p>
<div class="highlight-container" data-rel="Kotlin"><figure class="iseeu highlight kotlin"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">dependencies &#123;</span><br><span class="line">    classpath(libs.springBoot)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></div>
<h4 id="通过-toml-文件声明-version-catalogs"><a class="markdownIt-Anchor" href="#通过-toml-文件声明-version-catalogs">#</a> 通过 TOML 文件声明 version catalogs</h4>
<p>除了在  <code>settings.gradle</code>  文件中直接声明依赖目录，官方更推荐使用 TOML 文件来声明依赖目录</p>
<p>首先在项目根目录下创建  <code>libs.versions.toml</code>  文件，文件名可以任意取，并编写如下依赖内容：</p>
<div class="highlight-container" data-rel="Kotlin"><figure class="iseeu highlight kotlin"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">[versions]</span><br><span class="line">springBoot = <span class="string">&quot;3.0.0&quot;</span></span><br><span class="line">springCloud = <span class="string">&quot;2022.0.3&quot;</span></span><br><span class="line">springCloudAlibaba = <span class="string">&quot;2022.0.0.0-RC2&quot;</span></span><br><span class="line">lombok = <span class="string">&quot;1.18.28&quot;</span></span><br><span class="line"></span><br><span class="line">[libraries]</span><br><span class="line">springBoot = &#123;group = <span class="string">&quot;org.springframework.boot&quot;</span>, name = <span class="string">&quot;spring-boot-gradle-plugin&quot;</span>, version.ref = <span class="string">&quot;springBoot&quot;</span>&#125;</span><br><span class="line">springCloud = &#123;module = <span class="string">&quot;org.springframework.cloud:spring-cloud-dependencies&quot;</span>, version.ref = <span class="string">&quot;springCloud&quot;</span>&#125;</span><br><span class="line">springCloud-Alibaba = &#123;module = <span class="string">&quot;org.springframework.cloud:spring-cloud-dependencies&quot;</span>, version.ref = <span class="string">&quot;springCloudAlibaba&quot;</span>&#125;</span><br><span class="line">lombok = &#123;module = <span class="string">&quot;org.projectlombok:lombok&quot;</span>, version.ref = <span class="string">&quot;lombok&quot;</span>&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure></div>
<p>在  <code>settings.gradle.ktx</code>  引入文件，注意，默认值是 <code>libs.versions.toml</code> , 默认位置为 <code>project/gradle/</code> ，所以当在这个目录下且使用这个名字的时候不需要显式的引入</p>
<div class="highlight-container" data-rel="Kotlin"><figure class="iseeu highlight kotlin"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">dependencyResolutionManagement &#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 使用版本目录</span></span><br><span class="line">    versionCatalogs &#123;</span><br><span class="line">        create(<span class="string">&quot;libs&quot;</span>) &#123;</span><br><span class="line">            from(files(<span class="string">&quot;./gradle/libs.versions.toml&quot;</span>))</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></div>
<p>在 Gradle 中， <code>dependencyResolutionManagement</code>  块中的版本目录（Version Catalogs）通常是全局配置，不能在 <code>allprojects</code>  和 <code>subprojects</code>  块中直接使用。这是因为版本目录的配置是针对整个 Gradle 项目的，而不是针对单个项目或子项目的。</p>
<div class="highlight-container" data-rel="Kotlin"><figure class="iseeu highlight kotlin"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">dependencies &#123;</span><br><span class="line">    <span class="keyword">val</span> springCloudBom = platform(libs.springCloud.origin)</span><br><span class="line">    <span class="keyword">val</span> springCloudAlibabaBom = platform(libs.springCloud.alibaba)</span><br><span class="line"></span><br><span class="line">    implementation(springCloudBom)</span><br><span class="line">    implementation(springCloudAlibabaBom)</span><br><span class="line">    implementation(libs.lombok)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></div>
<p><img  
                     lazyload
                     src="/images/loading.svg"
                     data-src="http://tuchuang.wenhe9.cn/img/202309122348069.png"
                      alt="img.png"
                ></p>
<p>TOML 文件由 4 个主要部分组成</p>
<ul>
<li>[versions] 用于声明可以被依赖项引用的版本</li>
<li>[libraries] 用于声明依赖的别名</li>
<li>[bundles] 用于声明依赖包（依赖组）</li>
<li>[plugins] 用于声明插件</li>
</ul>

            </div>

            
                <div class="post-copyright-info">
                    <div class="article-copyright-info-container">
    <ul>
        <li><strong>标题:</strong> Gradle依赖管理的三种方式</li>
        <li><strong>作者:</strong> meteor</li>
        <li><strong>创建于:</strong> 2023-09-14 00:27:15</li>
        
            <li>
                <strong>更新于:</strong> 2023-09-13 08:30:49
            </li>
        
        <li>
            <strong>链接:</strong> https://gitee.com/du-jinliang/2023/09/14/Gradle依赖管理的三种方式/
        </li>
        <li>
            <strong>版权声明:</strong> 本文章采用 <a class="license" target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh">CC BY-NC-SA 4.0</a> 进行许可。
        </li>
    </ul>
</div>

                </div>
            

            
                <ul class="post-tags-box">
                    
                        <li class="tag-item">
                            <a href="/tags/Gradle/">#Gradle</a>&nbsp;
                        </li>
                    
                </ul>
            

            

            
                <div class="article-nav">
                    
                    
                        <div class="article-next">
                            <a class="next"
                            rel="next"
                            href="/2023/09/02/MVCC%E5%BA%95%E5%B1%82%E5%8E%9F%E7%90%86/"
                            >
                                <span class="title flex-center">
                                    <span class="post-nav-title-item">MVCC底层原理</span>
                                    <span class="post-nav-item">下一篇</span>
                                </span>
                                <span class="right arrow-icon flex-center">
                                    <i class="fa-solid fa-chevron-right"></i>
                                </span>
                            </a>
                        </div>
                    
                </div>
            


            
                <div class="comment-container">
                    <div class="comments-container">
    <div id="comment-anchor"></div>
    <div class="comment-area-title">
        <i class="fa-solid fa-comments"></i>&nbsp;评论
    </div>
    

        
            
    <div id="gitalk-container"></div>
    <script data-pjax
            src="//cdn.jsdelivr.net/npm/gitalk/dist/gitalk.min.js"></script>
    <script data-pjax>

        function loadGitalk() {
            let __gitalk__pathname = decodeURI(location.pathname);
            const __gitalk__pathnameLength = __gitalk__pathname.length;
            const __gitalk__pathnameMaxLength = 50;
            if (__gitalk__pathnameLength > __gitalk__pathnameMaxLength) {
                __gitalk__pathname = __gitalk__pathname.substring(0, __gitalk__pathnameMaxLength - 3) + '...';
            }

            try {
                Gitalk && new Gitalk({
                    clientID: '55bad54a77b7e60ad62d',
                    clientSecret: '1031c81500c6be06e338087cb7b713f2d0201b46',
                    repo: 'wait-you.github.io',
                    owner: 'wait-you',
                    admin: ['wait-you'],
                    id: 'comment',
                    language: 'zh-CN'
                }).render('gitalk-container');

            } catch (e) {
                window.Gitalk = null;
            }
        }

        if ('true') {
            const loadGitalkTimeout = setTimeout(() => {
                loadGitalk();
                clearTimeout(loadGitalkTimeout);
            }, 1000);
        } else {
            window.addEventListener('DOMContentLoaded', loadGitalk);
        }
    </script>



        
    
</div>

                </div>
            
        </div>

        
            <div class="toc-content-container">
                <div class="post-toc-wrap">
    <div class="post-toc">
        <div class="toc-title">此页目录</div>
        <div class="page-title">Gradle依赖管理的三种方式</div>
        <ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#gradle%E4%BE%9D%E8%B5%96%E7%AE%A1%E7%90%86%E7%9A%84%E4%B8%89%E7%A7%8D%E6%96%B9%E5%BC%8F"><span class="nav-text"> Gradle 依赖管理的三种方式</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#ext"><span class="nav-text"> ext</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#buildsrc"><span class="nav-text"> buildSrc</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#tomlversion-catalogs"><span class="nav-text"> toml–Version Catalogs</span></a></li></ol></li></ol>

    </div>
</div>
            </div>
        
    </div>
</div>


                

            </div>
            
            

        </div>

        <div class="main-content-footer">
            <footer class="footer">
    <div class="info-container">
        <div class="copyright-info">
            &copy;
            
              <span>2022</span>
              -
            
            2023&nbsp;&nbsp;<i class="fa-solid fa-heart fa-beat" style="--fa-animation-duration: 0.5s; color: #f54545"></i>&nbsp;&nbsp;<a href="/">meteor</a>
        </div>
        
            <script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
            <div class="website-count info-item">
                
                    <span id="busuanzi_container_site_uv" class="busuanzi_container_site_uv">
                        访问人数&nbsp;<span id="busuanzi_value_site_uv" class="busuanzi_value_site_uv"></span>
                    </span>
                
                
                    <span id="busuanzi_container_site_pv" class="busuanzi_container_site_pv">
                        总访问量&nbsp;<span id="busuanzi_value_site_pv" class="busuanzi_value_site_pv"></span>
                    </span>
                
            </div>
        
        <div class="theme-info info-item">
            <span class="powered-by-container">由 <?xml version="1.0" encoding="utf-8"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg version="1.1" id="圖層_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="1rem" height="1rem" viewBox="0 0 512 512" enable-background="new 0 0 512 512" xml:space="preserve"><path fill="#0E83CD" d="M256.4,25.8l-200,115.5L56,371.5l199.6,114.7l200-115.5l0.4-230.2L256.4,25.8z M349,354.6l-18.4,10.7l-18.6-11V275H200v79.6l-18.4,10.7l-18.6-11v-197l18.5-10.6l18.5,10.8V237h112v-79.6l18.5-10.6l18.5,10.8V354.6z"/></svg><a target="_blank" href="https://hexo.io">Hexo</a> 驱动</span>
                <br>
            <span class="theme-version-container">主题&nbsp;<a class="theme-version" target="_blank" href="https://github.com/EvanNotFound/hexo-theme-redefine">Redefine v2.1.4</a>
        </div>
        
            <div class="icp-info info-item"><a target="_blank" rel="nofollow" href="
                
                    beian.miit.gov.cn
                
                ">冀ICP备20010108号</a></div>
        
        
        
            <div id="start_div" style="display:none">
                2022/8/17 11:45:14
            </div>
            <div>
                博客已运行 <span class="odometer" id="runtime_days" ></span> 天 <span class="odometer" id="runtime_hours"></span> 小时 <span class="odometer" id="runtime_minutes"></span> 分钟 <span class="odometer" id="runtime_seconds"></span> 秒
            </div>
        
        
        
            <script async data-pjax>
                try {
                    function odometer_init() {
                    const elements = document.querySelectorAll('.odometer');
                    elements.forEach(el => {
                        new Odometer({
                            el,
                            format: '( ddd).dd',
                            duration: 200
                        });
                    });
                    }
                    odometer_init();
                } catch (error) {}
            </script>
        
        
        
    </div>  
</footer>
        </div>
    </div>

    
        <div class="post-tools">
            <div class="post-tools-container">
    <ul class="article-tools-list">
        <!-- TOC aside toggle -->
        
            <li class="right-bottom-tools page-aside-toggle">
                <i class="fa-regular fa-outdent"></i>
            </li>
        

        <!-- go comment -->
        
            <li class="go-comment">
                <i class="fa-regular fa-comments"></i>
            </li>
        
    </ul>
</div>

        </div>
    

    <div class="right-side-tools-container">
        <div class="side-tools-container">
    <ul class="hidden-tools-list">
        <li class="right-bottom-tools tool-font-adjust-plus flex-center">
            <i class="fa-regular fa-magnifying-glass-plus"></i>
        </li>

        <li class="right-bottom-tools tool-font-adjust-minus flex-center">
            <i class="fa-regular fa-magnifying-glass-minus"></i>
        </li>

        <li class="right-bottom-tools tool-expand-width flex-center">
            <i class="fa-regular fa-expand"></i>
        </li>

        <li class="right-bottom-tools tool-dark-light-toggle flex-center">
            <i class="fa-regular fa-moon"></i>
        </li>

        <!-- rss -->
        

        

        <li class="right-bottom-tools tool-scroll-to-bottom flex-center">
            <i class="fa-regular fa-arrow-down"></i>
        </li>
    </ul>

    <ul class="visible-tools-list">
        <li class="right-bottom-tools toggle-tools-list flex-center">
            <i class="fa-regular fa-cog fa-spin"></i>
        </li>
        
            <li class="right-bottom-tools tool-scroll-to-top flex-center">
                <i class="arrow-up fas fa-arrow-up"></i>
                <span class="percent"></span>
            </li>
        
        
    </ul>
</div>

    </div>

    <div class="image-viewer-container">
    <img src="">
</div>


    


</main>




<script src="/js/utils.js"></script>

<script src="/js/main.js"></script>

<script src="/js/layouts/navbarShrink.js"></script>

<script src="/js/tools/scrollTopBottom.js"></script>

<script src="/js/tools/lightDarkSwitch.js"></script>





    
<script src="/js/tools/codeBlock.js"></script>




    
<script src="/js/layouts/lazyload.js"></script>




    
<script src="/js/tools/runtime.js"></script>

    
<script src="/js/layouts/odometer.min.js"></script>

    
<link rel="stylesheet" href="/assets/odometer-theme-minimal.css">




  
<script src="/js/libs/Typed.min.js"></script>

  
<script src="/js/plugins/typed.js"></script>







<div class="post-scripts pjax">
    
        
<script src="/js/tools/tocToggle.js"></script>

<script src="/js/libs/anime.min.js"></script>

<script src="/js/layouts/toc.js"></script>

<script src="/js/plugins/tabs.js"></script>

    
</div>


    
<script src="/js/libs/pjax.min.js"></script>

<script>
    window.addEventListener('DOMContentLoaded', () => {
        window.pjax = new Pjax({
            selectors: [
                'head title',
                '.page-container',
                '.pjax',
            ],
            history: true,
            debug: false,
            cacheBust: false,
            timeout: 0,
            analytics: false,
            currentUrlFullReload: false,
            scrollRestoration: false,
            // scrollTo: true,
        });

        document.addEventListener('pjax:send', () => {
            Global.utils.pjaxProgressBarStart();
        });

        document.addEventListener('pjax:complete', () => {
            Global.utils.pjaxProgressBarEnd();
            window.pjax.executeScripts(document.querySelectorAll('script[data-pjax], .pjax script'));
            Global.refresh();
        });
    });
</script>




</body>
</html>
